<?php

namespace App\Models;

use Dcat\Admin\Traits\HasDateTimeFormatter;

use Illuminate\Database\Eloquent\Model;

class WxUniversity extends Model
{
	use HasDateTimeFormatter;
    protected $table = 'wx_university';

    protected $primaryKey = 'sid';

    public $timestamps = false;

    public $incrementing = false;

    protected $keyType = 'string';

    protected static function boot()
    {
        parent::boot();

        static::creating(function ($model) {
            // 在模型创建之前触发的操作
        });

        static::created(function ($model) {

        });

        static::updating(function ($model) {
            // 在模型更新之前触发的操作
            if($model->sid == '-1'){
                throw new \Exception('全域 不可修改');
            }
            $model->original = $model->getOriginal();
        });

        static::updated(function ($model) {
//            $original = $model->original;
//            $changes = $model->getChanges();
        });

        static::deleting(function ($model) {
            if($model->sid == '-1'){
                throw new \Exception('全域 不可删除');
            }
        });

        static::deleted(function ($model) {

        });
        // 其他的模型事件监听...
    }

    public function getClassTimeAttribute($v)
    {
        global $__MINI_GLOBAL_REQUEST_ENV__;
        if(_empty_($v)){
            if($__MINI_GLOBAL_REQUEST_ENV__ == 'api'){
                return json_decode('[{"startTime":"08:00","endTime":"08:40"},{"startTime":"08:45","endTime":"09:25"},{"startTime":"09:40","endTime":"10:20"},{"startTime":"10:35","endTime":"11:15"},{"startTime":"11:20","endTime":"12:00"},{"startTime":"13:30","endTime":"14:10"},{"startTime":"14:15","endTime":"14:55"},{"startTime":"15:10","endTime":"15:50"},{"startTime":"15:55","endTime":"16:35"},{"startTime":"18:30","endTime":"19:10"},{"startTime":"19:20","endTime":"20:00"},{"startTime":"20:20","endTime":"20:50"}]', true);
            }else{
                return '';
            }
        }
        if($__MINI_GLOBAL_REQUEST_ENV__ == 'api'){
            $class_arr = json_decode($v, true);
            if($class_arr){
                for ($i = 0; $i < count($class_arr); $i++){
                    $startTime = explode(':', $class_arr[$i]['startTime']);
                    $class_arr[$i]['startTime'] = $startTime[0].':'.$startTime[1];

                    $endTime = explode(':', $class_arr[$i]['endTime']);
                    $class_arr[$i]['endTime'] = $endTime[0].':'.$endTime[1];
                }
                return $class_arr;
            }
            return [];
        }else{
            return $v;
        }
    }

}
